<template>
  <el-dialog
    title="编辑邮箱"
    width="500px"
    @close="cancel"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="form" :rules="rules" ref="Form" label-width="120px">
      <el-form-item label="发件人邮箱" prop="form">
        <el-input v-model="form.form"></el-input>
      </el-form-item>
      <el-form-item label="发件用户名" prop="user">
        <el-input v-model="form.user"></el-input>
      </el-form-item>
      <el-form-item label="邮箱密码" prop="password">
        <el-input v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item label="SMTP地址" prop="host">
        <el-input v-model="form.host"></el-input>
      </el-form-item>
      <el-form-item label="SMTP端口" prop="port">
        <el-input v-model="form.port"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <submit-button ref="Submit" @submit="submit"/>
    </div>
  </el-dialog>
</template>

<script>
  import {editEmailApi} from '@/api/tools/email';
  import {resetForm} from "@/utils/common";

  export default {
    name: "Edit",
    data() {
      return {
        visible: false,
        form: {
          id: null,
          form: '',
          user: '',
          password: '',
          host: '',
          port: ''
        },
        rules: {
          form: {required: true, message: '请输入发件人邮箱', trigger: 'blur'},
          user: {required: true, message: '请输入发件用户名', trigger: 'blur'},
          password: {required: true, message: '请输入邮箱密码', trigger: 'blur'},
          host: {required: true, message: '请输入SMTP地址', trigger: 'blur'},
          port: {required: true, message: '请输入SMTP端口', trigger: 'blur'}
        }
      }
    },
    methods: {
      submit() {
        this.$refs['Form'].validate(valid => {
          if (!valid) return false;
          let data = {...this.form};
          this.$refs.Submit.start();
          editEmailApi(data).then(result => {
            this.$refs.Submit.stop();
            if (result.status !== 200) return;
            this.$emit('update');
            this.visible = false;
          }).catch(() => {
            this.$refs.Submit.stop();
          })
        });
      },
      cancel() {
        resetForm(this)
      }
    }
  }
</script>
